﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class backmanage_API_A_News : System.Web.UI.Page
{
    /// <summary>
    /// NEWSSYSTEM NEWS.API 新闻系统API
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        string editType = Request.Params["edit"];//操作类型
        string returnStr = string.Empty;
        switch (editType)
        {
            case "add":
                {
                    returnStr = NewsAdd();
                    break;
                }
            case "del":
                {
                    returnStr = NewsDel();
                    break;
                }
            case "update":
                {
                    returnStr = NewsUpdate();
                    break;
                }
            case "list":
                {
                    returnStr = NewsList();
                    break;
                }
            case "ishot":
                {
                    returnStr = UpdateIshot();
                    break;
                }
            case "classid":
                {
                    returnStr = UpdateClassid();
                    break;
                }
            case "title":
                {
                    returnStr = UpdateTitle();
                    break;
                }
            case "class":
                {
                    returnStr = ClassList();
                    break;
                }
            case "item":
                {
                    returnStr = GetInfoItem();
                    break;
                }
            default: break;
        }
        Response.Write(returnStr);
    }
    /// <summary>
    /// 添加新闻
    /// </summary>
    /// <returns></returns>
    protected string NewsAdd()
    {
        MODEL.NewsInformation model = new MODEL.NewsInformation();
        BLL.NewsInformation bll = new BLL.NewsInformation();
        model.NID = GetId();
        model.NclassID = Convert.ToInt32(Request.Params["classid"]);
        model.Ntitle = Request.Params["title"];
        model.Nkey = Request.Params["keyword"];
        model.Nsubtitle = Request.Params["subtitle"];
        model.Nabstract = Request.Params["abstracr"];
        model.Nauthor = Request.Params["author"];
        model.Ncontent = Request.Params["content"];
        model.Nisfrom = Request.Params["isfrom"];
        model.Nisimg = Convert.ToBoolean(Session["isImg"]);
        model.Nishot = Convert.ToBoolean(Convert.ToInt32(Request.Params["ishot"]));
        model.Nurl = "null";
        model.Noperator = "10001";
        model.NclickNum = 0;
        return bll.NewsInformationAdd(model).ToString();
        
    }
    /// <summary>
    /// 删除新闻
    /// </summary>
    /// <returns></returns>
    protected string NewsDel()
    {
        string[] idList = Request.Params["newsid[]"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        string errId = string.Empty;
        for (int i = 0; i < idList.Length; i++)
        {
            BLL.NewsInformation bll = new BLL.NewsInformation();
            bool flag = bll.NewsInformationDel(idList[i]);
            if (flag)
            {
                errId = "True";
            }
            else
            {
                errId += idList[i] + ",";
            }
        }
        return errId;
    }
    /// <summary>
    /// 新闻列表
    /// </summary>
    /// <returns></returns>
    protected string NewsList()
    {
        BLL.NewsInformation bll = new BLL.NewsInformation();
        DataSet ds = bll.GetNewsInformationList();
        StringBuilder htmlStr = new StringBuilder();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string isTop,isHot;
            if (Convert.ToBoolean(ds.Tables[0].Rows[i]["Nisimg"]))
            { isTop = "是"; }
            else { isTop = "否"; }
            if (Convert.ToBoolean(ds.Tables[0].Rows[i]["Nishot"]))
            { isHot = "是"; }
            else { isHot = "否"; }
            string className = GetClassName(Convert.ToInt32(ds.Tables[0].Rows[i]["Nclassid"]));
            htmlStr.Append("<tr id=\"" + ds.Tables[0].Rows[i]["Nid"].ToString() + "\">");
            htmlStr.Append("<td>");
            htmlStr.Append("<input type=\"checkbox\" value=\"" + ds.Tables[0].Rows[i]["Nid"].ToString() + "\">");
            htmlStr.Append("</td>");
            htmlStr.Append("<td><a href=\"#\" title=\""+ds.Tables[0].Rows[i]["Nishot"].ToString()+"\" class=\"hot\">" + isHot + "</a></td>");
            htmlStr.Append("<td>" + isTop + "</td>");
            htmlStr.Append("<td><a href=\"#\" title=\"" + ds.Tables[0].Rows[i]["Nclassid"].ToString() + "\" class=\"classid\">" + className + "</a></td>");
            htmlStr.Append("<td><a href=\"#\" class=\"text\">" + ds.Tables[0].Rows[i]["Ntitle"].ToString() + "</a></td>");
            htmlStr.Append("<td>" + ds.Tables[0].Rows[i]["Ntime"].ToString() + "</td>");
            htmlStr.Append("<td><a href=\"#\" title=\"" + ds.Tables[0].Rows[i]["Nid"].ToString() + "\" class=\"edit\">编辑</a></td>");
            htmlStr.Append("</tr>");
        }
        return htmlStr.ToString();
    }
    /// <summary>
    /// 获取最大ID
    /// </summary>
    /// <returns></returns>
    protected string GetId()
    {
        BLL.NewsInformation bll=new BLL.NewsInformation();
        string timeStr = DateTime.Now.ToString("yyyyMMdd");
        string maxId = string.Format("{0:D3}",Convert.ToInt32(bll.GetMaxId()) + 1);
        return timeStr + maxId;
    }
    /// <summary>
    /// 修改是否热点
    /// </summary>
    /// <returns></returns>
    protected string UpdateIshot()
    {
        BLL.NewsInformation bll = new BLL.NewsInformation();
        string newId = Request.Params["id"];
        bool isHot = Convert.ToBoolean(Request.Params["ishot"]);
        return bll.NewsInformationUpdateIshot(newId, isHot).ToString();
    }
    /// <summary>
    /// 修改新闻类别
    /// </summary>
    /// <returns></returns>
    protected string UpdateClassid()
    {
        BLL.NewsInformation bll = new BLL.NewsInformation();
        string newId = Request.Params["id"];
        int classId = Convert.ToInt32(Request.Params["classid"]);
        return bll.NewsInformationUpdateClassid(newId, classId).ToString();
    }
    /// <summary>
    /// 修改新闻标题
    /// </summary>
    /// <returns></returns>
    protected string UpdateTitle()
    {
        BLL.NewsInformation bll = new BLL.NewsInformation();
        string newId = Request.Params["id"];
        string newTitle = Request.Params["title"];
        return bll.NewsInformationUpdateTitle(newId, newTitle).ToString();
    }
    /// <summary>
    /// 新闻列别列表
    /// </summary>
    /// <returns></returns>
    protected string ClassList()
    {
        BLL.NewsColumn bll = new BLL.NewsColumn();
        DataSet ds = bll.GetNewsColumnList();
        StringBuilder htmlStr=new StringBuilder();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            htmlStr.Append("<option value=\"" + ds.Tables[0].Rows[i]["Nclassid"].ToString() + "\">" + ds.Tables[0].Rows[i]["Nclassname"].ToString() + "</option>");
        }
        return htmlStr.ToString();
    }
    /// <summary>
    /// 获取类别名称
    /// </summary>
    /// <param name="classId"></param>
    /// <returns></returns>
    protected string GetClassName(int classId)
    {
        MODEL.NewsColumn model = new MODEL.NewsColumn();
        BLL.NewsColumn bll = new BLL.NewsColumn();
        model = bll.GetNewsColumnModel(classId);
        return model.NclassName;
    }
    /// <summary>
    /// 获取新闻信息
    /// </summary>
    /// <returns></returns>
    protected string GetInfoItem()
    {
        StringBuilder returnData = new StringBuilder();
        MODEL.NewsInformation model = new MODEL.NewsInformation();
        BLL.NewsInformation bll = new BLL.NewsInformation();
        model = bll.GetNewsInformationModel(Request.Form["id"]);
        returnData.Append("{");
        returnData.Append("\"t\":\"" + model.Ntitle + "\",");
        returnData.Append("\"c\":\"" + model.NclassID + "\",");
        returnData.Append("\"k\":\"" + model.Nkey + "\",");
        returnData.Append("\"s\":\"" + model.Nsubtitle + "\",");
        returnData.Append("\"a\":\"" + model.Nabstract + "\",");
        returnData.Append("\"au\":\"" + model.Nauthor + "\",");
        returnData.Append("\"f\":\"" + model.Nisfrom + "\",");
        returnData.Append("\"h\":\"" + model.Nishot.ToString().ToLower() + "\",");
        returnData.Append("\"co\":\"" + model.Ncontent + "\"");
        returnData.Append("}");
        return returnData.ToString();
    }
    /// <summary>
    /// 修改新闻内容
    /// </summary>
    /// <returns></returns>
    protected string NewsUpdate()
    {
        MODEL.NewsInformation model = new MODEL.NewsInformation();
        BLL.NewsInformation bll = new BLL.NewsInformation();
        model.NID = Request.Form["id"];
        model.NclassID = Convert.ToInt32(Request.Form["classid"]);
        model.Ntitle = Request.Form["title"];
        model.Nkey = Request.Form["keyword"];
        model.Nsubtitle = Request.Form["subtitle"];
        model.Nabstract = Request.Form["abstracr"];
        model.Nauthor = Request.Form["author"];
        model.Ncontent = Request.Form["content"];
        model.Nisfrom = Request.Form["isfrom"];
        model.Nishot = Convert.ToBoolean(Convert.ToInt32(Request.Form["ishot"]));
        return bll.NewsInformationUpdate(model).ToString();
    }
}
